package cn.edu.nuc.weiboclient;
import java.util.HashMap;
import java.util.Map;
import oauth.signpost.OAuth;
import cn.edu.nuc.modle.WeiBoList;
import com.google.gson.Gson;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class WeiboClientActivity extends Activity {
	final String TAG = getClass().getName();
	private SharedPreferences prefs;
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem followsItem = menu.add(Menu.NONE, 1, 1, "Friends");
		MenuItem mainItem = menu.add(Menu.NONE, 0, 0, "ME");
		MenuItem friendsItem = menu.add(Menu.NONE, 2, 2, "Funs");
		MenuItem homepageItem = menu.add(Menu.NONE, 3, 3, "@ME");
		MenuItem otherUsersItem = menu.add(Menu.NONE,4, 4, "Enter");
		MenuItem usermentionedItem = menu.add(Menu.NONE, 5, 5, "Exit");
		
		followsItem.setIcon(android.R.drawable.ic_menu_send);
		friendsItem.setIcon(android.R.drawable.ic_menu_more);
		homepageItem.setIcon(android.R.drawable.ic_menu_send);
		otherUsersItem.setIcon(android.R.drawable.ic_menu_more);
		usermentionedItem.setIcon(android.R.drawable.ic_menu_send);
		mainItem.setIcon(android.R.drawable.ic_media_rew);
		return super.onCreateOptionsMenu(menu);
	}
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		if(item.getItemId() == 5){
			finish();
			System.exit(0);
		}
		else if(item.getItemId() == 1){
			Intent intent = new Intent();
			intent.setClass(this,FriendsTimeLineActivity.class);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 2){
			Intent intent = new Intent();
			intent.setClass(this, FollowsTimeLine.class);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 3){
			Intent intent = new Intent();
			intent.setClass(this, UsermentionedTimeLine.class);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 4){
			Intent intent = new Intent();
			intent.setClass(this,Enter.class).setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 0){
			Intent intent = new Intent();
			intent.setClass(this,MyPostTimeLine.class);
			startActivity(intent);
			finish();
		}
		return super.onMenuItemSelected(featureId, item);
	}
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.test);
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		ImageButton launchOauth = (ImageButton) findViewById(R.id.loginInterface);
        ImageButton broadcastTimelineButton = (ImageButton)findViewById(R.id.home);
        broadcastTimelineButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Map<String,String> keyValues = new HashMap<String,String>();
				keyValues.put("format", "json");
				keyValues.put("pageflag", "0");
				keyValues.put("pagetime", "0");
				keyValues.put("reqnum", "20");
				WeiBoClient weiBoClient = new WeiBoClient(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET, prefs.getString(OAuth.OAUTH_TOKEN, ""), prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""));
				String result = weiBoClient.doGet(Constants.WeiBoApi.HOME_TIMELINE, keyValues);
				System.out.println("result--->" + result);
				Gson gson = new Gson();
				WeiBoList weiBoList = gson.fromJson(result, WeiBoList.class);
				System.out.println("weibolist---->" + weiBoList);
				Intent intent = new Intent();
				intent.setClass(WeiboClientActivity.this, HomepageTimeLine.class);
				startActivity(intent);
				finish();
			}
		});        
        
        launchOauth.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	startActivity(new Intent().setClass(v.getContext(), PrepareRequestTokenActivity.class));
            }
        });
	}
}	